﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class InputManager : Singleton<InputManager> 
{
	protected InputManager () {}
	
	public enum Button 
	{ 
		A = 0, 
		B, 
		Y, 
		X, 
		R1,
		L1, 
		R3, 
		L3, 
		Select, 
		Start 
	}
	
	public enum Axis 
	{ 
		HORIZONTAL = 0,
		VERTICAL,
		TRIGGERS,
		SECONDARYHORIZONTAL,
		SECONDARYVERTICAL,
		DPADHORIZONTAL,
		DPADVERTICAL,
	}

	private class GamePad
	{
		public GamePad()
		{
		}
	}

	#region VARIABLES
	public static float MainAxisThreshold = 0.1f;
	#endregion

	//private Dictionary<int, GamePad> m_GamePads;

	protected override void Awake()
	{
		base.Awake();

		//m_GamePads = new Dictionary<int, GamePad>();
	}

	private void Update()
	{		
		CheckGamePadInputs();
	}

	private void CheckGamePadInputs()
	{
		/*
		for(int i = 0; i < Input.GetJoystickNames().Length; i++)
		{
			if(Input.GetKey(GetKeyCodeFromButton(i + 1, Button.A)))
			{
				//Debug.Log("A");
			}
			if(Input.GetKey(GetKeyCodeFromButton(i + 1, Button.B)))
			{
				//Debug.Log("B");
			}
			if(Input.GetKey(GetKeyCodeFromButton(i + 1, Button.X)))
			{
				//Debug.Log("X");
			}
			if(Input.GetKey(GetKeyCodeFromButton(i + 1, Button.Y)))
			{
				//Debug.Log("Y");
			}
			if(Input.GetKey(GetKeyCodeFromButton(i + 1, Button.R1)))
			{
				//Debug.Log("R1");
			}
			if(Input.GetKey(GetKeyCodeFromButton(i + 1, Button.L1)))
			{
				//Debug.Log("L1");
			}
			if(Input.GetKey(GetKeyCodeFromButton(i + 1, Button.Select)))
			{
				//Debug.Log("Select");
			}
			if(Input.GetKey(GetKeyCodeFromButton(i + 1, Button.Start)))
			{
				//Debug.Log("Start");
			}
			if(Input.GetKey(GetKeyCodeFromButton(i + 1, Button.R3)))
			{
				//Debug.Log("L3");
			}
			if(Input.GetKey(GetKeyCodeFromButton(i + 1, Button.L3)))
			{
				//Debug.Log("R3");
			}
		}
		*/
	}

	#region PUBLICS
	public static bool GetButton(int i_ID, Button i_Button)
	{		
		return Input.GetKey(GetKeyCodeFromButton(i_ID, i_Button));
	}
	public static bool GetButtonDown(int i_ID, Button i_Button)
	{		
		return Input.GetKeyDown(GetKeyCodeFromButton(i_ID, i_Button));
	}
	public static bool GetButtonUp(int i_ID, Button i_Button)
	{		
		return Input.GetKeyUp(GetKeyCodeFromButton(i_ID, i_Button));
	}

	public static float GetAxis(int i_ID, Axis i_Axis)
	{
		float value = 0f;
		switch(i_Axis)
		{
		case Axis.HORIZONTAL:
			value = Input.GetAxis("Joy" + i_ID.ToString() + "X");
			if(Mathf.Abs(value) <= MainAxisThreshold)
			{
				value = 0;
			}
			break;
		case Axis.VERTICAL:
			value = Input.GetAxis("Joy" + i_ID.ToString() + "Y");
			if(Mathf.Abs(value) <= MainAxisThreshold)
			{
				value = 0;
			}
			break;
		case Axis.TRIGGERS:
			value = Input.GetAxis("Joy" + i_ID.ToString() + "Triggers");
			break;
		case Axis.SECONDARYHORIZONTAL:
			value = Input.GetAxis("Joy" + i_ID.ToString() + "RightX");
			break;
		case Axis.SECONDARYVERTICAL:
			value = Input.GetAxis("Joy" + i_ID.ToString() + "RightY");
			break;
		case Axis.DPADHORIZONTAL:
			value = Input.GetAxis("Joy" + i_ID.ToString() + "DPadX");
			break;
		case Axis.DPADVERTICAL:
			value = Input.GetAxis("Joy" + i_ID.ToString() + "DPadY");
			break;
		}

		return value;
	}

	public static KeyCode GetKeyCodeFromButton(int i_ID, Button i_Button)
	{
		switch(i_ID)
		{
		case 1:
			switch (i_Button)
			{
			case Button.A: 		return KeyCode.Joystick1Button0;
			case Button.B: 		return KeyCode.Joystick1Button1;
			case Button.X: 		return KeyCode.Joystick1Button2;
			case Button.Y: 		return KeyCode.Joystick1Button3;
			case Button.R1: 	return KeyCode.Joystick1Button5;
			case Button.L1: 	return KeyCode.Joystick1Button4;
			case Button.Select: return KeyCode.Joystick1Button6;
			case Button.Start: 	return KeyCode.Joystick1Button7;
			case Button.L3: 	return KeyCode.Joystick1Button8;
			case Button.R3: 	return KeyCode.Joystick1Button9;
			}
			return KeyCode.None;
		case 2:
			switch (i_Button)
			{
			case Button.A: 		return KeyCode.Joystick2Button0;
			case Button.B: 		return KeyCode.Joystick2Button1;
			case Button.X: 		return KeyCode.Joystick2Button2;
			case Button.Y: 		return KeyCode.Joystick2Button3;
			case Button.R1: 	return KeyCode.Joystick2Button5;
			case Button.L1: 	return KeyCode.Joystick2Button4;
			case Button.Select: return KeyCode.Joystick2Button6;
			case Button.Start: 	return KeyCode.Joystick2Button7;
			case Button.L3: 	return KeyCode.Joystick2Button8;
			case Button.R3: 	return KeyCode.Joystick2Button9;
			}
			return KeyCode.None;
		case 3:
			switch (i_Button)
			{
			case Button.A: 		return KeyCode.Joystick3Button0;
			case Button.B: 		return KeyCode.Joystick3Button1;
			case Button.X: 		return KeyCode.Joystick3Button2;
			case Button.Y: 		return KeyCode.Joystick3Button3;
			case Button.R1: 	return KeyCode.Joystick3Button5;
			case Button.L1: 	return KeyCode.Joystick3Button4;
			case Button.Select: return KeyCode.Joystick3Button6;
			case Button.Start: 	return KeyCode.Joystick3Button7;
			case Button.L3: 	return KeyCode.Joystick3Button8;
			case Button.R3: 	return KeyCode.Joystick3Button9;
			}
			return KeyCode.None;
		case 4:
			switch (i_Button)
			{
			case Button.A: 		return KeyCode.Joystick4Button0;
			case Button.B: 		return KeyCode.Joystick4Button1;
			case Button.X: 		return KeyCode.Joystick4Button2;
			case Button.Y: 		return KeyCode.Joystick4Button3;
			case Button.R1: 	return KeyCode.Joystick4Button5;
			case Button.L1: 	return KeyCode.Joystick4Button4;
			case Button.Select: return KeyCode.Joystick4Button6;
			case Button.Start: 	return KeyCode.Joystick4Button7;
			case Button.L3: 	return KeyCode.Joystick4Button8;
			case Button.R3: 	return KeyCode.Joystick4Button9;
			}
			return KeyCode.None;
		}
		
		return KeyCode.None;
	}
	#endregion
}
